package com.qyhl.webtv.module_live.utils.ItemViewDelegete;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.entity.Image;
import com.qyhl.webtv.commonlib.entity.live.TeleTextLiveBean;
import com.qyhl.webtv.commonlib.utils.ResizableImageView;
import com.qyhl.webtv.commonlib.utils.view.expandableView.text.ExpandableTextView;
import com.qyhl.webtv.commonlib.utils.view.imagebrowser.MNImageBrowser;
import com.qyhl.webtv.module_live.R;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class NanBuIconItemDelegete implements ItemViewDelegate<TeleTextLiveBean> {
    private List<TeleTextLiveBean> liveBean;
    private Context context;

    public NanBuIconItemDelegete(Context context, List<TeleTextLiveBean> liveBeen) {
        this.context = context;
        this.liveBean = liveBeen;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.live_item_nanbu_icon;
    }

    @Override
    public boolean isForViewType(TeleTextLiveBean item, int position) {
        if ("0".equalsIgnoreCase(item.getType())) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void convert(ViewHolder holder, TeleTextLiveBean teleTextLiveBean, int position) {
        //设置时间轴
        TextView time = holder.getView(R.id.item_time);
        time.setText(DateUtils.teleTextDateFormat(teleTextLiveBean.getPublishtime()));//时间

        //设置作者
        TextView author = holder.getView(R.id.item_author);
        if (StringUtils.isEmpty(teleTextLiveBean.getAuthor())) {
            author.setVisibility(View.GONE);
        } else {
            author.setVisibility(View.VISIBLE);
            author.setText(StringUtils.encodePhoneNum(teleTextLiveBean.getAuthor()));
        }

        //设置简介
        ExpandableTextView content = holder.getView(R.id.item_content);
        if (StringUtils.isEmpty(teleTextLiveBean.getContent())) {
            content.setVisibility(View.GONE);
        } else {
            content.setVisibility(View.VISIBLE);
            content.setText(teleTextLiveBean.getContent(), position);
        }

        //设置日期
        TextView date = holder.getView(R.id.item_date);
        LinearLayout dateLayout = holder.getView(R.id.top_layout);
        if (position != 0) {
            if (liveBean.size() > 0) {
                if (DateUtils.isSameDay(teleTextLiveBean.getPublishtime(), liveBean.get(position - 1).getPublishtime())) {
                    dateLayout.setVisibility(View.GONE);
                } else {
                    dateLayout.setVisibility(View.VISIBLE);
                    date.setText(DateUtils.teleTextFormat(teleTextLiveBean.getPublishtime()));
                }
            } else {
                dateLayout.setVisibility(View.GONE);
            }
        } else {
            dateLayout.setVisibility(View.VISIBLE);
            date.setText(DateUtils.teleTextFormat(teleTextLiveBean.getPublishtime()));
        }
        //设置单图
        RecyclerView picRecycler = holder.getView(R.id.cover_recycler);
        picRecycler.setLayoutManager(new LinearLayoutManager(context));

        CommonAdapter<TeleTextLiveBean.Images> commonAdapter = new CommonAdapter<TeleTextLiveBean.Images>(context,R.layout.live_item_nanbu_pic,teleTextLiveBean.getImages()) {
            @Override
            protected void convert(ViewHolder holder, TeleTextLiveBean.Images img, int position) {
                ResizableImageView cover = holder.getView(R.id.cover);
                Glide.with(context)
                        .load(img.getUrl())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_large_default).error(R.drawable.cover_large_default))
                        .into(cover);
            }
        };

        picRecycler.setAdapter(commonAdapter);

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder viewHolder, int position) {
                List<Image> imageList = initDatas(teleTextLiveBean.getImages());
                ArrayList<String> mList = new ArrayList<>();
                for (int i = 0; i < imageList.size(); i++) {
                    mList.add(imageList.get(i).getUrl());
                }
                MNImageBrowser.showImageBrowser(context, view, position, mList);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                return false;
            }
        });
    }

    private List<Image> initDatas(List<TeleTextLiveBean.Images> beanList) {
        List<Image> images = new ArrayList<>();
        for (int i = 0; i < beanList.size(); i++) {
            images.add(new Image(beanList.get(i).getUrl(), 0, 0));
        }
        return images;
    }
}
